import matplotlib
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号

matplotlib.use('TkAgg')
class MplMgr:
    def __init__(self):
        pass
    def test(self):
        import numpy as np
        import matplotlib.pyplot as plt
        # 计算正弦和余弦曲线上的点的 x 和 y 坐标
        x = np.arange(0, 3 * np.pi, 0.1)
        y_sin = np.sin(x)
        y_cos = np.cos(x)
        # 建立 subplot 网格，高为 2，宽为 1
        # 激活第一个 subplot
        plt.subplot(2, 1, 1)
        # 绘制第一个图像
        plt.plot(x, y_sin)
        plt.title('Sine')
        # 将第二个 subplot 激活，并绘制第二个图像
        plt.subplot(2, 1, 2)
        plt.plot(x, y_cos)
        plt.title('Cosine')
        # 展示图像
        plt.show()

    def show_graph(self, dict_data):

        fig = plt.figure(figsize=(8, 6))

        ax1 = fig.add_subplot(2, 1, 1)
        ax2 = fig.add_subplot(2, 1, 2)
        #ax3 = fig.add_subplot(2, 2, 3)
        #ax4 = fig.add_subplot(2, 2, 4)

        df_equity = dict_data['equity']
        df_equity.plot(ax=ax1)



        df_years = dict_data['yearly']
        if df_years is not None:
            print(df_years)
            df_years.T.plot(kind='bar', ax=ax2, use_index=True)
            # sns.barplot(x, y, palette="BuPu_r")

        '''
        
        df_ratio = dict_data['ratio']
        if df_ratio is not None:
            print(df_ratio)
            table = ax3.table(cellText=df_ratio.values,
                                   cellLoc='center',
                                   cellColours=None,
                                   rowLabels=df_ratio.index,
                                   # rowColours=plt.cm.BuPu(np.linspace(0, 0.5, 5))[::-1],  # BuPu可替换成其他colormap
                                   colLabels=df_ratio.columns,
                                   # colColours=plt.cm.Reds(np.linspace(0, 0.5, 5))[::-1],
                                   rowLoc='right',
                                   loc='bottom',
                                   # bbox=[0, -0.4, 1, 0.4],
                                   )
            #table.scale(1, 2)
            #ax3.axis('off')
        

        df_corr = dict_data['corr']
        if df_corr is not None:
            # print(df_corr)
            # df_corr.plot(ax=self.ax4)
            sns.heatmap(data=df_corr,
                        annot=True,
                        # vmax=0.3,  # 上图颜色太深，不美观，让整体颜色变浅点
                        ax=ax4)
        '''
        plt.show()